package com.chen.service.impl;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.chen.entity.Test;
import com.chen.dao.TestMapper;
import com.chen.service.ITestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.List;

@Service
public class TestServiceImpl implements ITestService {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private TestMapper testMapper;

    @DS("slave")
    @Override
    public List<Test> getTestsFromSlave() {
        String datasourceLookUp =DynamicDataSourceContextHolder.getDataSourceLookupKey();
        logger.info("当前使用数据源：{}" ,datasourceLookUp);
        return testMapper.listTest();
    }
    @DS("master")
    @Override
    public List<Test> getTestsFromMaster() {
        String datasourceLookUp =DynamicDataSourceContextHolder.getDataSourceLookupKey();
        logger.info("当前使用数据源：{}" ,datasourceLookUp);
        return testMapper.listTest();
    }

    @Override
    public int add(Test test) {
        String datasourceLookUp =DynamicDataSourceContextHolder.getDataSourceLookupKey();
        logger.info("当前使用数据源：{}" ,datasourceLookUp);
        return testMapper.insert(test);
    }
}
